#include "custom.h"

Custom::Custom(){ }

QString Custom::lastStr(const QString &str){
    if(str.isEmpty())
        return "";
    return str[str.length()-1];
}

int Custom::subStringLength(const std::string &str)
{
    using namespace std;
    std::vector<int> rs;
    unordered_set<char> tStr;


    for (const auto &c: str) {
        if (tStr.empty()) {
            tStr.emplace(c);
            continue;
        }
        if (tStr.find(c)!=tStr.end()) {
            rs.emplace_back(tStr.size());
            tStr.clear();
            tStr.emplace(c);
            continue;
        }
        tStr.emplace(c);
    }
    rs.emplace_back(tStr.size());
    sort(rs.begin(), rs.end());
    return rs.back();
}

unordered_map<string, string > Custom::userInfos(const std::string &path)
{
    string filePath;
    filePath = path.empty()?"C:\\Users\\nidaye\\Documents\\qt5proj\\user\\userInfo.txt":path;
    unordered_map<string,string> users;
    string line;
    std::ifstream infile(filePath);
    if(!infile.good())
        return {};
    else{
        while(getline(infile,line)){
            auto index = line.find_first_of(",");
            users[line.substr(0,index)]=line.substr(index+1,line.size()-1);
        }
    }
    return users;
}
